package com.zhengyuedong.bbs.core.action;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;
import com.zhengyuedong.bbs.core.entity.User;
import com.zhengyuedong.bbs.core.service.UserService;
import com.zhengyuedong.bbs.core.util.HashUtil;

public class UserAction extends ActionSupport implements ServletRequestAware{
	private HttpServletRequest request;
	
	private UserService userService;
	
	private String loginName;
	private String displayName;
	private String password;
	private String email;
	private String url;
	private Date registerTime;
	private Integer status;
	private String activationKey;
	private String alias;

	public String reg() throws Exception {
		User user = new User();
		user.setEmail(email);
		user.setLoginName(email);
		user.setPassword(HashUtil.hash(password));
		user.setDisplayName(displayName);
		user.setRegisterTime(new Date());
		user.setStatus(0);
		
		String flag = userService.addUser(user);
		if ("success".equals(flag)) {
			request.getSession().setAttribute("currentUser", user);
		}
		return "regSuccess";
	}
	
	public String preReg() {
		return "reg";
	}
	
	public String login() {
		List<User> users = userService.findByLoginName(email);
		User currentUser = null;
		if (users != null) {
			currentUser = users.get(0);
		}
		request.getSession().setAttribute("currentUser", currentUser);
		return "loginSuccess";
	}

	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	public String getDisplayName() {
		return displayName;
	}

	public void setDisplayName(String displayName) {
		this.displayName = displayName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public Date getRegisterTime() {
		return registerTime;
	}

	public void setRegisterTime(Date registerTime) {
		this.registerTime = registerTime;
	}

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public String getActivationKey() {
		return activationKey;
	}

	public void setActivationKey(String activationKey) {
		this.activationKey = activationKey;
	}

	public String getAlias() {
		return alias;
	}

	public void setAlias(String alias) {
		this.alias = alias;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

}
